package com.szzg10.webservice.chapter5;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.xml.ws.BindingProvider;

public class RabbitCounterClient {

	public static void main(String[] args) throws KeyManagementException, NoSuchAlgorithmException, IOException {
		new RabbitCounterClient().doIt();
	}
	
	private void doIt() throws KeyManagementException, NoSuchAlgorithmException, IOException {
		SSLContext sslContext = SSLContext.getInstance("TLS");
		sslContext.init(null, getTrustManagers(), new SecureRandom());
		
		HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
		
		URL url = new URL("https://localhost:9876/fib");
		
		HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();
		conn.setHostnameVerifier(new HostnameVerifier() {
			@Override
			public boolean verify(String hostname, SSLSession session) {
				if(hostname.equals("localhost")) {
					return true;
				}
				else {
					return false;
				}
			}
		});
		
		conn.setDoInput(true);
		conn.setDoOutput(true);
		conn.setRequestMethod("POST");
		
		conn.addRequestProperty(BindingProvider.USERNAME_PROPERTY, "fred");
		conn.addRequestProperty(BindingProvider.PASSWORD_PROPERTY, "rockbed");
		
		conn.connect();
		
		List<Integer> nums = new ArrayList<Integer>();
		nums.add(3);
		nums.add(5);
		nums.add(7);
		
		OutputStream out = conn.getOutputStream();
		out.write(nums.toString().getBytes());
		
		byte[] bytes = new byte[4096];
		InputStream in = conn.getInputStream();
		
		String message = "";
		while(in.read(bytes) != -1) {
			message += new String(bytes);
		}
		
		System.out.println(message);
		System.out.println();
		
		dump_features(conn);
		
		conn.disconnect();
	}
	
	private TrustManager[] getTrustManagers() {
		TrustManager[] trustManagers = new TrustManager[] {
			new X509TrustManager() {
				@Override
				public X509Certificate[] getAcceptedIssuers() {
					return null;
				}
				
				@Override
				public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
				
				@Override
				public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
			}
		};
		
		return trustManagers;
	}
	
	private void dump_features(HttpsURLConnection conn) throws IOException {
		System.out.println("Response Code: " + conn.getResponseCode());
		System.out.println("Cipher Suite: " +conn.getCipherSuite());
		
		Certificate[] certificates = conn.getServerCertificates();
		for(Certificate element: certificates) {
			System.out.println("Type: " + element.getType());
			System.out.println("Hash Code: " + element.hashCode());
			System.out.println("Algorithm: " +element.getPublicKey().getAlgorithm());
			System.out.println("Format: " + element.getPublicKey().getFormat());
		}
	}
}
